FROM ubuntu:20.04

ENV RISCV=/opt/riscv
ENV PATH=$RISCV/bin:$PATH
WORKDIR $RISCV

COPY *.list /etc/apt/
COPY pip.* /etc/

RUN apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y

RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && \
    dpkg-reconfigure --frontend noninteractive tzdata

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y libnewlib-dev
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y git cmake

# install riscv-gnu-toolchain
RUN git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
RUN cd riscv-gnu-toolchain && ./configure --prefix=/opt/riscv --with-arch=rv64gc --enable-multilib && make -j8

ENV RISCV=/opt/riscv
ENV PATH=$RISCV/bin:$PATH

# install riscv-pk
RUN cd $RISCV/riscv-gnu-toolchain/pk && mkdir -p build && cd build && ../configure --prefix=$RISCV --host=riscv64-unknown-elf && make -j8 && make install

# install spike
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y device-tree-compiler
RUN cd $RISCV/riscv-gnu-toolchain/spike && mkdir -p build && cd build && ../configure --prefix=$RISCV && make -j8 && make install

# install qemu
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python3-pip
RUN pip3 install sphinx meson -i https://pypi.mirrors.ustc.edu.cn/simple
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential pkg-config libboost-all-dev autoconf libtool libssl-dev flex bison ninja-build libglib2.0-dev libpixman-1-dev libslirp-dev libncurses5-dev libncursesw5-dev libspice-protocol-dev libspice-server-dev libusbredirparser-dev libusb-1.0-0-dev
# sync qemu to latest
RUN cd $RISCV/riscv-gnu-toolchain/qemu && git fetch --all && git reset --hard origin/master
RUN cd $RISCV/riscv-gnu-toolchain/qemu && mkdir -p build && cd build && ../configure --prefix=$RISCV --target-list=riscv64-softmmu,riscv64-linux-user  --enable-kvm --enable-spice --enable-vnc --enable-guest-agent --enable-libusb --enable-usb-redir && make -j8 && make install

# install docker
RUN cd /tmp && curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh

# install other software
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y vim ssh htop

WORKDIR /root
